<template>
    <div class="login">
        <div class="login-box">
            <h2>欢迎登录</h2>
            <form>
                <div class="login-field">
                    <input type="text" v-model="loginForm.username" />
                    <label>用户名</label>
                </div>
                <div class="login-field">
                    <input type="password" v-model="loginForm.password" />
                    <label>密码</label>
                </div>
                <el-button :plain="false" type="button" @click="login">登&emsp;&emsp;录</el-button>
            </form>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            loginForm: {
                username: 'admin',
                password: '123456',
            }
        }
    },
    methods: {
        login() {
            this.axios.post('http://localhost:80/user/login', {
                userName: this.loginForm.username,
                password: this.loginForm.password
            })
                .then(response => {
                    alert(response.data.message);
                    console.log(response.data.data);
                    if (response.data.code == 200 && response.data.message == "success") {
                        //1.登录成功后，拿到一个凭证并存起来
                        // 取的时候 用  JSON.parse(data.data);解析
                        window.localStorage.setItem("token_key", JSON.stringify(response.data.data));//整个对象序列化
                        //2.存储完token之后，才跳转页面
                        this.$router.replace({
                            path: '/dashboard'
                        });
                        this.$message({
                            showClose: true,
                            message: '登录成功',
                            type: 'success'
                        });
                    } else {
                        this.$message({
                            showClose: true,
                            message: '登录失败',
                            type: 'error'
                        });
                    }
                })
                .then(function (error) {
                    console.log(error);
                });
        },
    },
}
</script>

<style>
body {
    margin: 0;
    padding: 0;
    font-family: sans-serif;
    background: url(../assets/rbacBg1.jpg) no-repeat center 0px;
    background-size: cover;
    background-position: center 0;
    background-repeat: no-repeat;
    background-attachment: fixed;
}

.login-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 400px;
    padding: 40px;
    background: rgba(0, 0, 0, .4);
    box-sizing: border-box;
    box-shadow: 0 15px 25px rgba(0, 0, 0, .25);
    border-radius: 10px;
}

.login-box h2 {
    margin: 0 0 30px;
    padding: 0;
    text-align: center;
    color: #fff;
}

.login-box .login-field {
    position: relative;
}

.login-box .login-field input {
    width: 100%;
    padding: 10px 0;
    font-size: 16px;
    color: #fff;
    margin-bottom: 30px;
    border: none;
    border-bottom: 1px solid #fff;
    outline: none;
    background: transparent;
}

.login-box .login-field label {
    position: absolute;
    top: 0;
    left: 0;
    letter-spacing: 1px;
    padding: 10px 0;
    font-size: 16px;
    color: #fff;
    pointer-events: none;
    transition: .5s;
}

.login-box .login-field input:focus~label,
.login-box .login-field input:valid~label {
    top: -23px;
    left: 0;
    color: aqua;
    font-size: 12px;
}

.login-box button {
    background: transparent;
    margin: 0 auto;
    border: none;
    outline: none;
    color: #fff;
    background: rgba(42, 33, 202, 0.6);
    padding: 10px 40px;
    cursor: pointer;
    border-radius: 5px;
}
</style>
